{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9ae83a90-e687-41ca-90a6-5b34560ad539",
   "metadata": {},
   "source": [
    "# Parameter `arrow` in `geomSpoke()`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fd991535-210f-405d-9dbe-873409ace7dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"nkCRMq\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "       if(!window.letsPlotCallQueue) {\n",
       "           window.letsPlotCallQueue = [];\n",
       "       }; \n",
       "       window.letsPlotCall = function(f) {\n",
       "           window.letsPlotCallQueue.push(f);\n",
       "       };\n",
       "       (function() {\n",
       "           var script = document.createElement(\"script\");\n",
       "           script.type = \"text/javascript\";\n",
       "           script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v4.3.1/js-package/distr/lets-plot.min.js\";\n",
       "           script.onload = function() {\n",
       "               window.letsPlotCall = function(f) {f();};\n",
       "               window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "               window.letsPlotCallQueue = [];\n",
       "               \n",
       "               \n",
       "           };\n",
       "           script.onerror = function(event) {\n",
       "               window.letsPlotCall = function(f) {};\n",
       "               window.letsPlotCallQueue = [];\n",
       "               var div = document.createElement(\"div\");\n",
       "               div.style.color = 'darkred';\n",
       "               div.textContent = 'Error loading Lets-Plot JS';\n",
       "               document.getElementById(\"nkCRMq\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"nkCRMq\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b51b5d33-3dc4-42f1-8ac6-ab9aedd494d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Lets-Plot Kotlin API v.4.7.1. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.3.1."
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LetsPlot.getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "76db93e3-b101-46ce-a1ff-46b14752f286",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun linspace(start: Double, stop: Double, num: Int): List<Double> {\n",
    "    if (num <= 0) return emptyList()\n",
    "    if (num == 1) return listOf(start)\n",
    "    val step = (stop - start) / (num - 1)\n",
    "    return List(num) { start + it * step }\n",
    "}\n",
    "\n",
    "fun simpleMeshgrid(xs: List<Double>, ys: List<Double>): Pair<List<List<Double>>, List<List<Double>>> {\n",
    "    return Pair(\n",
    "        List(ys.size) { xs },\n",
    "        ys.map { y -> List(xs.size) { y } }\n",
    "    )\n",
    "}\n",
    "\n",
    "fun cartesianToPolar(xArray: List<Double>, yArray: List<Double>): Pair<List<Double>, List<Double>> {\n",
    "    val rArray = xArray.zip(yArray).map { (x, y) -> sqrt(x * x + y * y) }\n",
    "    val maxR = rArray.maxOrNull() ?: 0.0\n",
    "    val normalizedR = rArray.map { it / maxR }\n",
    "    val theta = xArray.zip(yArray).map { (x, y) -> atan2(y, x) }\n",
    "    return Pair(normalizedR, theta)\n",
    "}\n",
    "\n",
    "fun f(x: List<Double>, y: List<Double>) = y to x.map { -it }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "eec0580c-4838-4ef4-b93f-81f11f9fcb24",
   "metadata": {},
   "outputs": [],
   "source": [
    "val n = 11\n",
    "val start = -5.0\n",
    "val stop = 5.0\n",
    "val space = linspace(start, stop, n)\n",
    "val (X, Y) = simpleMeshgrid(space, space)\n",
    "val (R, A) = f(X.flatten(), Y.flatten()).let { cartesianToPolar(it.first, it.second) }\n",
    "val data = mapOf(\n",
    "    \"x\" to X.flatten(),\n",
    "    \"y\" to Y.flatten(),\n",
    "    \"r\" to R,\n",
    "    \"a\" to A\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "200c16f5-77af-43c1-ab7f-0a9ded817499",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"Htr4ls\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\",\n",
       "\"color\":\"r\"\n",
       "},\n",
       "\"coord\":{\n",
       "\"name\":\"fixed\",\n",
       "\"xlim\":[-5.0,5.0],\n",
       "\"flip\":false,\n",
       "\"ylim\":[-5.0,5.0]\n",
       "},\n",
       "\"data\":{\n",
       "\"a\":[2.356194490192345,2.4668517113662407,2.601173153319209,2.761086276477428,2.9441970937399127,-3.141592653589793,-2.9441970937399127,-2.761086276477428,-2.601173153319209,-2.4668517113662407,-2.356194490192345,2.2455372690184494,2.356194490192345,2.498091544796509,2.677945044588987,2.896613990462929,-3.141592653589793,-2.896613990462929,-2.677945044588987,-2.498091544796509,-2.356194490192345,-2.2455372690184494,2.1112158270654806,2.214297435588181,2.356194490192345,2.5535900500422257,2.819842099193151,-3.141592653589793,-2.819842099193151,-2.5535900500422257,-2.356194490192345,-2.214297435588181,-2.1112158270654806,1.9513027039072617,2.0344439357957027,2.1587989303424644,2.356194490192345,2.677945044588987,-3.141592653589793,-2.677945044588987,-2.356194490192345,-2.1587989303424644,-2.0344439357957027,-1.9513027039072617,1.7681918866447774,1.8157749899217608,1.892546881191539,2.0344439357957027,2.356194490192345,-3.141592653589793,-2.356194490192345,-2.0344439357957027,-1.892546881191539,-1.8157749899217608,-1.7681918866447774,1.5707963267948966,1.5707963267948966,1.5707963267948966,1.5707963267948966,1.5707963267948966,-0.0,-1.5707963267948966,-1.5707963267948966,-1.5707963267948966,-1.5707963267948966,-1.5707963267948966,1.373400766945016,1.3258176636680326,1.2490457723982544,1.1071487177940904,0.7853981633974483,-0.0,-0.7853981633974483,-1.1071487177940904,-1.2490457723982544,-1.3258176636680326,-1.373400766945016,1.1902899496825317,1.1071487177940904,0.982793723247329,0.7853981633974483,0.4636476090008061,-0.0,-0.4636476090008061,-0.7853981633974483,-0.982793723247329,-1.1071487177940904,-1.1902899496825317,1.0303768265243125,0.9272952180016122,0.7853981633974483,0.5880026035475675,0.3217505543966422,-0.0,-0.3217505543966422,-0.5880026035475675,-0.7853981633974483,-0.9272952180016122,-1.0303768265243125,0.8960553845713439,0.7853981633974483,0.6435011087932844,0.4636476090008061,0.24497866312686414,-0.0,-0.24497866312686414,-0.4636476090008061,-0.6435011087932844,-0.7853981633974483,-0.8960553845713439,0.7853981633974483,0.6747409422235527,0.5404195002705842,0.3805063771123649,0.19739555984988078,-0.0,-0.19739555984988078,-0.3805063771123649,-0.5404195002705842,-0.6747409422235527,-0.7853981633974483],\n",
       "\"r\":[1.0,0.9055385138137416,0.8246211251235321,0.7615773105863908,0.7211102550927978,0.7071067811865475,0.7211102550927978,0.7615773105863908,0.8246211251235321,0.9055385138137416,1.0,0.9055385138137416,0.8,0.7071067811865475,0.6324555320336759,0.58309518948453,0.565685424949238,0.58309518948453,0.6324555320336759,0.7071067811865475,0.8,0.9055385138137416,0.8246211251235321,0.7071067811865475,0.6,0.5099019513592784,0.4472135954999579,0.4242640687119285,0.4472135954999579,0.5099019513592784,0.6,0.7071067811865475,0.8246211251235321,0.7615773105863908,0.6324555320336759,0.5099019513592784,0.4,0.31622776601683794,0.282842712474619,0.31622776601683794,0.4,0.5099019513592784,0.6324555320336759,0.7615773105863908,0.7211102550927978,0.58309518948453,0.4472135954999579,0.31622776601683794,0.2,0.1414213562373095,0.2,0.31622776601683794,0.4472135954999579,0.58309518948453,0.7211102550927978,0.7071067811865475,0.565685424949238,0.4242640687119285,0.282842712474619,0.1414213562373095,0.0,0.1414213562373095,0.282842712474619,0.4242640687119285,0.565685424949238,0.7071067811865475,0.7211102550927978,0.58309518948453,0.4472135954999579,0.31622776601683794,0.2,0.1414213562373095,0.2,0.31622776601683794,0.4472135954999579,0.58309518948453,0.7211102550927978,0.7615773105863908,0.6324555320336759,0.5099019513592784,0.4,0.31622776601683794,0.282842712474619,0.31622776601683794,0.4,0.5099019513592784,0.6324555320336759,0.7615773105863908,0.8246211251235321,0.7071067811865475,0.6,0.5099019513592784,0.4472135954999579,0.4242640687119285,0.4472135954999579,0.5099019513592784,0.6,0.7071067811865475,0.8246211251235321,0.9055385138137416,0.8,0.7071067811865475,0.6324555320336759,0.58309518948453,0.565685424949238,0.58309518948453,0.6324555320336759,0.7071067811865475,0.8,0.9055385138137416,1.0,0.9055385138137416,0.8246211251235321,0.7615773105863908,0.7211102550927978,0.7071067811865475,0.7211102550927978,0.7615773105863908,0.8246211251235321,0.9055385138137416,1.0],\n",
       "\"x\":[-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[-5.0,-5.0,-5.0,-5.0,-5.0,-5.0,-5.0,-5.0,-5.0,-5.0,-5.0,-4.0,-4.0,-4.0,-4.0,-4.0,-4.0,-4.0,-4.0,-4.0,-4.0,-4.0,-3.0,-3.0,-3.0,-3.0,-3.0,-3.0,-3.0,-3.0,-3.0,-3.0,-3.0,-2.0,-2.0,-2.0,-2.0,-2.0,-2.0,-2.0,-2.0,-2.0,-2.0,-2.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,4.0,4.0,4.0,4.0,4.0,4.0,4.0,4.0,4.0,4.0,4.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"color\",\n",
       "\"scale_mapper_kind\":\"color_gradient\",\n",
       "\"high\":\"#d53e4f\",\n",
       "\"low\":\"#3288bd\",\n",
       "\"guide\":\"none\"\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"angle\":\"a\",\n",
       "\"radius\":\"r\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"arrow\":{\n",
       "\"name\":\"arrow\",\n",
       "\"angle\":12.0,\n",
       "\"length\":15.0,\n",
       "\"type\":\"closed\"\n",
       "},\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"spoke\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"minimal\",\n",
       "\"axis_text\":{\n",
       "\"margin\":10.0,\n",
       "\"blank\":false\n",
       "},\n",
       "\"axis_title\":\"blank\"\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"Htr4ls\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letsPlot(data) { x = \"x\"; y = \"y\"; color = \"r\" } +\n",
    "    geomSpoke(arrow = arrow(type = \"closed\", angle = 12, length = 15)) { angle = \"a\"; radius = \"r\" } +\n",
    "    scaleColorGradient(low = \"#3288bd\", high = \"#d53e4f\", guide = \"none\") +\n",
    "    coordFixed(xlim = start to stop, ylim = start to stop) +\n",
    "    themeMinimal() +\n",
    "    theme(axisText = elementText(margin = 10), axisTitle = \"blank\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
